package weichuanbo.com.mywebserver;

import android.os.Environment;
import android.text.TextUtils;
import android.webkit.MimeTypeMap;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import fi.iki.elonen.NanoHTTPD;

public class HttpServer extends NanoHTTPD {
    private static final String TAG = "HttpServer";

    public static final String DEFAULT_SHOW_PAGE = "index.html";
    public static final int DEFAULT_PORT = 9511;//此参数随便定义，最好定义1024-65535；1-1024是系统常用端口,1024-65535是非系统端口

    public enum Status implements Response.IStatus {
        REQUEST_ERROR(500, "请求失败"),
        REQUEST_ERROR_API(501, "无效的请求接口"),
        REQUEST_ERROR_CMD(502, "无效命令");

        private final int requestStatus;
        private final String description;

        Status(int requestStatus, String description) {
            this.requestStatus = requestStatus;
            this.description = description;
        }

        @Override
        public String getDescription() {
            return description;
        }

        @Override
        public int getRequestStatus() {
            return requestStatus;
        }
    }

    public HttpServer() {//初始化端口
        super(DEFAULT_PORT);
    }

    @Override
    public Response serve(IHTTPSession session) {
        String uri = session.getUri();
        Map<String, String> headers = session.getHeaders();

        //接收不到post参数的问题，              http://blog.csdn.net/obguy/article/details/53841559
        try {
            session.parseBody(new HashMap<String, String>());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ResponseException e) {
            e.printStackTrace();
        }
        Map<String, String> parms = session.getParms();
        try {
            LogUtil.d(TAG, uri);

//判断uri的合法性，自定义方法，这个是判断是否是接口的方法
//            if (checkUri(uri)) {
                if (1==1) {
                // 针对的是接口的处理
                if (headers != null) {
                    LogUtil.d(TAG, headers.toString());
                }
                if (parms != null) {
                    LogUtil.d(TAG, parms.toString());
                }

                if (TextUtils.isEmpty(uri)) {
                    throw new RuntimeException("无法获取请求地址");
                }

                if (Method.OPTIONS.equals(session.getMethod())) {
                    LogUtil.d(TAG, "OPTIONS探测性请求");
                    return addHeaderResponse(Response.Status.OK);
                }

                switch (uri) {
                    /*case "/test": {//接口2
                        //此方法包括了封装返回的接口请求数据和处理异常以及跨域
                        return getXXX(parms);
                    }*/
                    default: {
                        return addHeaderResponse(Status.REQUEST_ERROR_API);
                    }
                }
            } else {
                //针对的是静态资源的处理
                String filePath =/* getFilePath(uri)*/""; // 根据url获取文件路径

                if (filePath == null) {
                    LogUtil.d(TAG, "sd卡没有找到");
                    return super.serve(session);
                }
                File file = new File(filePath);

                if (file != null && file.exists()) {
                    LogUtil.d(TAG, "file path = " + file.getAbsolutePath());
//根据文件名返回mimeType： image/jpg, video/mp4, etc
                    String mimeType = getMimeType(file);

                    Response res = null;
                    InputStream is = new FileInputStream(file);
                    res = newFixedLengthResponse(Response.Status.OK, mimeType, is, is.available());
//下面是跨域的参数（因为一般要和h5联调，所以最好设置一下）
                  /*  res.addHeader("Access-Control-Allow-Headers", allowHeaders);
                    res.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, HEAD");
                    res.addHeader("Access-Control-Allow-Credentials", "true");
                    res.addHeader("Access-Control-Allow-Origin", "*");
                    res.addHeader("Access-Control-Max-Age", "" + 42 * 60 * 60);*/

                    return res;
                } else {
                    LogUtil.d(TAG, "file path = " + file.getAbsolutePath() + "的资源不存在");
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        //自己封装的返回请求
        return addHeaderResponse(Status.REQUEST_ERROR);
    }

   /* private String getFilePath(String uri) {
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
        {
            Environment.getExternalStorageDirectory()+"/"+applicationInfo.packageName;
        }

    }*/

    private Response addHeaderResponse(Response.IStatus requestError) {
        return newFixedLengthResponse(requestError.getDescription());
    }


    private String getSuffix(File file) {
        if (file == null || !file.exists() || file.isDirectory()) {
            return null;
        }
        String fileName = file.getName();
        if (fileName.equals("") || fileName.endsWith(".")) {
            return null;
        }
        int index = fileName.lastIndexOf(".");
        if (index != -1) {
            return fileName.substring(index + 1).toLowerCase(Locale.US);
        } else {
            return null;
        }
    }

    public  String getMimeType(File file){
        String suffix = getSuffix(file);
        if (suffix == null) {
            return "file/*";
        }
        String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(suffix);
        if (type != null || !type.isEmpty()) {
            return type;
        }
        return "file/*";
    }

}